#include <bits/stdc++.h>

using namespace std;

const int N=1e5+10,MOD=1000000007;
typedef unsigned long long LL;

LL n,la,lb,len;
LL a[N],b[N],c[N],f[N];

int main() 
{
	cin>>n;
	cin>>la;
	for(int i=la;i>=1;i--) cin>>a[i];
	cin>>lb; len=max(la,lb);
	for(int i=lb;i>=1;i--) cin>>b[i]; 
	
	//for(int i=1;i<=la;i++) cout<<a[i]<<" ";
	for(int i=1;i<=la;i++) c[i]=a[i]+1;
	for(int i=1;i<=lb;i++) 
	{
		c[i]=max(c[i],b[i]+1);
		c[i]=c[i]>1?c[i]:2;
    c[i]=c[i]<=n?c[i]:n;
	}
	//	for(int i=1;i<=len;i++) cout<<c[i]<<" ";
	c[0]=f[0]=1;
	for(int i=1;i<=len;i++)
	{
		f[i]=f[i-1]*c[i-1];
	}
	//for(int i=1;i<=len;i++) cout<<f[i]<<" ";
	
	LL ta=0;
	for(int i=1;i<=la;i++) ta=(ta+a[i]*f[i])%MOD;
	LL tb=0;
	for(int i=1;i<=lb;i++) tb=(tb+b[i]*f[i])%MOD;
 	
 	cout<<(ta-tb+MOD)%MOD<<endl; 
	return 0;
}
